package com.tinckay.controller;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.entity.po.PumpInfo;
import com.tinckay.service.PumpInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

import java.util.List;

/**
 * <p>
 * 泵车基本信息 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-11-06
 */
@Api(tags = "泵车管理")
@RestController
@RequestMapping("/pumpInfo")
public class PumpInfoController extends ApiController {

    @Autowired
    private PumpInfoService pumpInfoService;

    @Log("删除泵车信息")
    @DeleteMapping
    @ApiOperation("删除泵车信息")
    //@RequiresPermissions("pumpInfo:delete")
    public R delete(@ApiParam("泵车Code数组") @RequestParam String[] codes) {
        if (ArrayUtil.isEmpty(codes)) {
            return failed("参数id不能为空.");
        }
        for (String code : codes) {
            pumpInfoService.removeById(code);
        }
        return success(null);
    }

    @Log("新增泵车信息")
    @PostMapping
    @ApiOperation("新增泵车信息")
    //@RequiresPermissions("pumpInfo:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) PumpInfo pumpInfo) {
        return success(pumpInfoService.savePumpInfo(pumpInfo));
    }

    @Log("更新泵车信息")
    @PutMapping
    @ApiOperation("更新泵车信息")
    //@RequiresPermissions("pumpInfo:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) PumpInfo pumpInfo) {
        return success(pumpInfoService.updatePumpInfo(pumpInfo));
    }

    @GetMapping("/list")
    @ApiOperation("分页查询泵车信息列表")
    //@RequiresPermissions("pumpInfo:list")
    public R<IPage> list(@RequestParam(required = false) String factory,
                         @RequestParam(required = false) String code,
                         @RequestParam(required = false) String model,
                         @RequestParam(required = false) String optUser,
                         @RequestParam(defaultValue = "1") int current,
                         @RequestParam(defaultValue = "10") int size) {
        IPage<PumpInfo> page = pumpInfoService.page(new Page<>(current, size),
                new QueryWrapper<PumpInfo>()
                        .lambda()
                        .eq(StrUtil.isNotBlank(factory), PumpInfo::getFactory, factory)
                        .eq(StrUtil.isNotBlank(code), PumpInfo::getCode, code)
                        .eq(StrUtil.isNotBlank(model), PumpInfo::getModel, model)
                        .eq(StrUtil.isNotBlank(optUser), PumpInfo::getOptUser, optUser));
        return success(page);
    }

    @GetMapping("/info/{pumpInfoId}")
    @ApiOperation("获取泵车信息")
    //@RequiresPermissions("pumpInfo:info")
    public R<PumpInfo> info(@PathVariable("pumpInfoId") String code) {
        return success(pumpInfoService.getById(code));
    }

    @GetMapping("/allPumpCode")
    @ApiOperation("查询所有泵车code")
    //@RequiresPermissions("pumpInfo:allPumpCode")
    public R allPumpCode() {
        List<String> result = pumpInfoService.allPumpCode();
        return success(result);
    }

}

